<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <!-- 添加分类按钮区域 -->
      <el-row>
        <el-col>
          <el-button @click="addopt" class="treetable" type="primary"
            >添加分类</el-button
          >
        </el-col>
      </el-row>
      <!-- 分类表格  -->
      <tree-table
        show-index
        index-text="#"
        border
        :show-row-hover="false"
        :expand-type="false"
        :data="cateList"
        :columns="columns"
        :selection-type="false"
      >
        <!-- 是否有效 -->
        <template slot="isok" slot-scope="scope">
          <i
            class="el-icon-success"
            v-if="scope.row.cat_deleted === false"
            style="color: lightgreen"
          ></i>
          <i class="el-icon-error" v-else style="color: lightred"></i>
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag
            type="success"
            size="mini"
            v-else-if="scope.row.cat_level === 1"
            >二级</el-tag
          >
          <el-tag type="warning" size="mini" v-else>三级</el-tag>
        </template>

        <!-- 操作 -->
        <template slot="opt" slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-s-operation"
            >编辑</el-button
          >
          <el-button size="mini" type="danger" icon="el-icon-delete-solid"
            >删除</el-button
          >
        </template>
      </tree-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>

      <!-- 添加加分类 -->
      <el-dialog
        title="添加分类"
        @close="addcateclose"
        :visible.sync="adddialogVisible"
        width="50%"
      >
        <el-form
          :rules="addcateFormRules"
          ref="addcateFormRef"
          :model="addcateForm"
          label-width="80px"
        >
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="addcateForm.cat_name"></el-input>
          </el-form-item>
          <el-form-item label="父级分类">
            <el-cascader
              size="small"
              expand-trigger="hover"
              v-model="selectkeys"
              :options="parentcatelist"
              :props="cascaderProps"
              @change="parentcateChange"
              clearable
              change-on-select
            ></el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="adddialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addcate">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>
<style lang="less" scoped>
.treetable {
  margin-bottom: 15px;
}
.el-cascader {
  width: 100%;
}
</style>

<script>
export default {
  data() {
    return {
      // 商品分类数据列表
      cateList: [],
      //查询分类数据的条件
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      //保存总数据条数
      total: 0,
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name',
        },
        {
          label: '是否有效?',
          type: 'template',
          template: 'isok',
        },
        {
          label: '排序',
          type: 'template',
          template: 'order',
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt',
        },
      ],
      adddialogVisible: false,
      addcateForm: {
        cat_name: '',
        // 父级分类的id
        cat_pid: 0,
        // 分类等级
        cat_level: 0,
      },
      addcateFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
        ],
      },
      parentcatelist: [],
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
      },
      // 选中的父级分类的id数组
      selectkeys: [],
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.$http.get('categories', {
        params: this.queryInfo,
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表数据失败')
      }
      //将数据列表赋值给cateList
      this.cateList = res.data.result
      //保存总数据条数
      this.total = res.data.total
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getCateList()
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getCateList()
    },
    addopt() {
      this.getparentcatelist()
      this.adddialogVisible = true
    },
    async getparentcatelist() {
      const { data: res } = await this.$http.get('categories', {
        params: {
          type: 2,
        },
      })
      if (res.meta.status != 200) {
        return this.$message.error('获取失败')
      }
      // console.log(res.data);
      this.parentcatelist = res.data
      // console.log(this.parentcatelist)
    },
    // 选择项发生变化触发这个函数
    parentcateChange() {
      // console.log(this.selectkeys);
      // console.log(a);
      if (this.selectkeys.length > 0) {
        this.addcateForm.cat_pid = this.selectkeys[this.selectkeys.length - 1]
        this.addcateForm.cat_level = this.selectkeys.length
        return
      } else {
        this.addcateForm.cat_pid = 0
        this.addcateForm.cat_level = 0
      }
    },
    addcate() {
      // console.log(this.addcateForm);
      this.$refs.addcateFormRef.validate(async (valid) => {
        if (!valid) return
        const { data: res } = await this.$http.post(
          'categories',
          this.addcateForm
        )
        console.log(res)
        if (res.meta.status !== 201) return this.$message.error('添加分类失败')
        this.$message.success('添加成功')
        this.getCateList()
        this.adddialogVisible = false
      })
    },
    addcateclose() {
      this.$refs.addcateFormRef.resetFields()
      this.selectkeys = []
      this.addcateForm.cat_pid = 0
      this.addcateForm.cat_level = 0
    },
  },
}
</script>